package edu.tms.dao;

import java.util.List;
import java.util.Map;

import edu.tms.bean.ActivityEnroll;
import edu.tms.crud.JdbcUtil;
import edu.tms.dao.common.CommnonDao;


/**
 * 报名DAO
 * @author 报名信息表
 *
 */
public class ActivityEnrollDaoImp implements CommnonDao<ActivityEnroll>{
	/**
	 * 用于保存一个报名信息
	 */
	@Override
	public long save(ActivityEnroll value) throws Exception {
		ActivityEnroll enroll=null;
		enroll=(ActivityEnroll)value;
		String sql="INSERT INTO t_activity_enroll(class_activity_id"
				+ ",stu_id,create_time,enroll_state) "
				+ "VALUES(?,?,?,?)";
		long num=JdbcUtil.insertAuto(sql,
				enroll.getClassActivityId(),
				enroll.getStuId(),
				enroll.getCreateTime(),
				enroll.getEnrollState());
		return num;
	}
	/**
	 * 修改报名信息
	 * 可以修改报名状态
	 */
	@Override
	public int update(ActivityEnroll value) throws Exception {
		ActivityEnroll enroll=null;
		enroll=(ActivityEnroll)value;
		 String sql="UPDATE t_activity_enroll "
		 		+ "SET class_activity_id=?,stu_id=?,"
		 		+ "create_time=?,enroll_state=? WHERE enroll_id=?";
		int num=JdbcUtil.update(sql, 
				enroll.getClassActivityId(),
				enroll.getStuId(),
				enroll.getCreateTime(),
				enroll.getEnrollState(),
				enroll.getEnrollId()
				);
		return num;
	}
	/**
	 * 删除
	 * 
	 */
	@Override
	public int delete(ActivityEnroll value) throws Exception {
		String sql="DELETE FROM t_activity_enroll WHERE enroll_id = ?";
		ActivityEnroll enroll=(ActivityEnroll)value;
		return JdbcUtil.update(sql,enroll.getEnrollId());
	}
	
	/**
	 * 审核操作
	 */
	public int updateState(int enrollId,int enrollState) throws Exception {
		String sql="UPDATE t_activity_enroll "
		 		+ "SET enroll_state=? "
		 		+ "WHERE enroll_id=?";
		return  JdbcUtil.update(sql, 
					enrollState,
					enrollId);
	}
	
	/**
	 * 查找报名信息
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<ActivityEnroll> findAll(int pageNo, int pageSize) throws Exception {
		String sql="SELECT * FROM t_activity_enroll limit ?,?";
		return JdbcUtil.query(ActivityEnroll.class, sql, (pageNo-1)*pageSize,pageSize);
	}
	
	/**
	 * 根据条件查询很多记录
	 * 性别，状态，类型，创建时间
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<ActivityEnroll> findAll(Object key, int pageNo, int pageSize) throws Exception {
		String sql="SELECT * FROM t_activity_enroll tae,"
				+ "t_class_activity tca,t_student ts WHERE "
				+ "tae.class_activity_id=tca.class_activity_id"
				+ " AND tae.stu_id=ts.stu_id AND tca.class_id=? LIMIT ?,?";
		return JdbcUtil.query(ActivityEnroll.class, sql,key, (pageNo-1)*pageSize,pageSize);
	}
	
	public long getPages(Object key, int pageSize) throws Exception {
		String sql="SELECT count(*) FROM t_activity_enroll tae,"
				+ "t_class_activity tca,t_student ts WHERE "
				+ "tae.class_activity_id=tca.class_activity_id"
				+ " AND tae.stu_id=ts.stu_id AND tca.class_id=?";
		long count=(long) JdbcUtil.uniqueValue(sql, 1, key);
		return count%pageSize==0?count/pageSize:count/pageSize+1;
	}
	
	/**
	 * 查找单一内容
	 */
	@Override
	public ActivityEnroll findByKey(Object key) throws Exception {
		return (ActivityEnroll) JdbcUtil.uniqueBean(ActivityEnroll.class,
				"select * from t_admin where enroll_id = ?",key, key);
	}
	
	public ActivityEnroll findByUid(Integer stuId,Integer activityid) throws Exception {
		return (ActivityEnroll) JdbcUtil.uniqueBean(ActivityEnroll.class,
				"select * from t_activity_enroll where stu_id = ? and class_activity_id=?",
				stuId,activityid);
	}
	
	/**
	 * String 对应  数据库 列名（comment)
	 * Object 对应相应列名值
	 * 返回的是所查找的类中的数据。方便EXCEL导出
	 */
	@Override
	public List<Map<String,Object>> getMapList() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
}
